/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean;

import entity.Employee;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletException;
import manager.EmployeeManager;
//import session.stateless.CustomerSessionBeanLocal;
import session.stateless.EmployeeSessionBeanLocal;

/**
 *
 * @author Vincent
 */
@ManagedBean(name="controller") 
@SessionScoped
public class Controller {

//    @Size(min=2, max=30) @Pattern(regexp = "[\\s\\w-,!\\.]*", message = "must match a-z,A-Z,0-9 or !,-_.") 
//    private String username; 
//    @Size(min=2, max=20) @Pattern(regexp = "[\\s\\w-,!\\.]*", message = "must match a-z,A-Z,0-9 or !,-_.") 
//    private String password; 
    
    private String message; 
    private String userName; 
    private String password;
    private String userType;
    private Employee employee;
    
    @EJB
    private EmployeeSessionBeanLocal employeeSessionBeanLocal;
    
//    @EJB
//    private CustomerSessionBeanLocal customerSessionBeanLocal;
    
    public String getMessage() { 
        return message; 
    } 


    public void setMessage(String message) { 
        this.message = message; 
    } 


    public String getUserName () { 
        return userName; 
    } 


    public void setUserName (String username) { 
        this.userName = username; 
    } 


    public String getPassword () { 
        return password; 
    } 


    public void setPassword (String password) { 
        this.password = password; 
    }

    public String getUserType() {
        return userType;
    }

    public void setUserType(String userType) {
        this.userType = userType;
    }

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }


//    public String CheckValidUser() throws ValidatorException { 
//        if(userName.equals("admin") && password.equals("admin")){ 
//            return "/start.xhtml"; 
//        } 
//        else{ 
//            String msgText = "Wrong username or password"; 
//            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, msgText, null); 
//            throw new ValidatorException(msg); 
//        } 
//    }
    
    public void login(){
        
        String temp_UserName = String.valueOf(userName);
        String temp_Password = String.valueOf(password);
        String temp_UserType = String.valueOf(userType);
        
        if (temp_UserType.equals("staff")){
            try { 
                EmployeeManager employeeManager = new EmployeeManager(employeeSessionBeanLocal);
                
                employee = employeeManager.find(userName, password);
                if (employee != null){
                    //this.userName = this.password = null;
                    FacesContext.getCurrentInstance().getExternalContext().redirect("./SuccessLogin");
                } 
                else{
                    //setMessage("Unknown login, please retry.");
                    FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");
                }
                
            } 
            catch(Exception ex) 
            { 
                ex.printStackTrace(); 
            } 
        }
        
//        if (temp_UserType.equals("customer")){
//            try { 
//                CustomerManager customerManager = new CustomerManager(customerSessionBeanLocal);
//
//                customerId = customerManager.verifyLogin(userName, password);
//                if (customerId == null){
//                }
//                else{
//                    FacesContext.getCurrentInstance().getExternalContext().redirect("innersystem/indexCustomer.xhtml");
//                } 
//            } 
//            catch(Exception ex) 
//            { 
//                ex.printStackTrace(); 
//            } 
//        }
    }
    
    public void logOut() throws ServletException {
        this.employee = null;
        
        ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
            try {
                ec.redirect(".././index.xhtml");
                System.out.print("abc");
            } catch (Exception ex) {
                Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
            }
        

//        try { 
//            //FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("username");
//            FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");
//        }
//                    catch(Exception ex) 
//            { 
//                ex.printStackTrace(); 
//            } 
    }
    
    public boolean isLoggedIn(){
        return (employee != null);
    }
    
    public boolean isNotLoggedIn(){
        return (employee == null);
    }
}
